﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CdcSoftware.Pivotal.Applications.Core.Client;
using System.Data;
using CdcSoftware.Pivotal.Applications.Core.Common;
using CdcSoftware.Pivotal.Engine.Client.Services.Interfaces;
using CdcSoftware.Pivotal.Engine.UI.Forms;


namespace Pcm.Retail.Client.FormTasks
{
    public partial class RE_RAE_Transaction : FormClientTask
    {
        #region Constructor
        /// <summary>
        /// Default constructor where secondary fields are being initialized to be used across assembly
        /// </summary>
        public RE_RAE_Transaction()
        {

        }
        #endregion

        public override void OnFormInitialized()
        {
            try
            {
                Globals.RemoveButtonBarButton(new string[] { "Save", "Apply", "Cancel", "Delete", "Refresh" });
                this.FormView.OnDirtyStateChanged += new EventHandler<Microsoft.Practices.CompositeUI.Utility.DataEventArgs<bool>>(FormView_OnDirtyStateChanged);
                SharedLoad();
                
            }
            catch (Exception exc)
            {
                Globals.HandleException(exc, true);
            }
        }

        public override void OnFormReloaded()
        {
            try
            {
                base.OnFormReloaded();
                SharedLoad();
            }
            catch(Exception ex)
            {
                Globals.HandleException(ex, true);
            }
        }

        private void FormView_OnDirtyStateChanged(object sender, Microsoft.Practices.CompositeUI.Utility.DataEventArgs<bool> e)
        {
            this.FormView.Dirty = false;
        }

        private void SharedLoad()
        {
            try
            {
                //AS - 18/9/2013 - Disable entire form when order status is not "Processed".
                //NN 19092013 - Addition check for RecordId and OrderProductId added as Airport counters and HQ will not have the Disc_Order_Status set.
                if (this.RecordId != null && this.PrimaryDataRow["SFA_Order_Product_Id"] != System.DBNull.Value &&
                    !String.Equals(this.PrimaryDataRow["Disc_Order_Status"], "Processed"))
                {
                    Globals.EnableDisableAllFields(false);
                    this.FormControl.GetControlByName("PivotalButton1").Enabled = false;
                }
                else
                {
                    if (!Convert.IsDBNull(this.PrimaryDataRow["Agreement_Number"]))
                    {
                        Globals.EnableDisableAllPrimaryFields(false);
                    }
                    else
                    {
                        if (String.Equals(TypeConvert.ToString(this.PrimaryDataRow["Source_"]), "Town Stores"))
                        {
                            Globals.EnableDisableAllPrimaryFields(false);
                        }
                        else
                        {
                            if (String.Equals(TypeConvert.ToString(this.PrimaryDataRow["RAE_Transaction"]), "CancelAEOrder"))
                            {
                                Globals.EnableDisablePrimaryFields(false, new string[] { "Contact_First_Name", "Contact_Last_Name", "Contact_Email",
                             "Date_of_Purchase"});
                                ((PivotalTextBox)this.FormControl.GetControlByDataName("Contact_First_Name")).Required = false;
                                ((PivotalTextBox)this.FormControl.GetControlByDataName("Contact_Last_Name")).Required = false;
                                ((PivotalTextBox)this.FormControl.GetControlByDataName("Contact_Email")).Required = false;
                                ((PivotalTextBox)this.FormControl.GetControlByDataName("Date_of_Purchase")).Required = false;
                            }
                            else
                            {
                                Globals.EnableDisablePrimaryFields(true, new string[] { "Contact_First_Name", "Contact_Last_Name", "Contact_Email",
                            "Date_of_Purchase"});
                                ((PivotalTextBox)this.FormControl.GetControlByDataName("Contact_First_Name")).Required = true;
                                ((PivotalTextBox)this.FormControl.GetControlByDataName("Contact_Last_Name")).Required = true;
                                ((PivotalTextBox)this.FormControl.GetControlByDataName("Contact_Email")).Required = true;
                                ((PivotalTextBox)this.FormControl.GetControlByDataName("Date_of_Purchase")).Required = true;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Globals.HandleException(ex, true);
            }
        }
    }
}
